require(['monitor', 'gaodeMap', 'highcharts', 'gauge'], function () {

    $('#monitor').monitor({
        canId: '12',
        callback: function (data) {
            loadData(data);
        },
        clearData: function () {
        	loadData({});
        }
    });
    
	// 高德地图对象
    var  bus = null;
    var map = new MapGaoDe(107.49023438, 36.24427318, 2);
    $('#iCenter').css({
        width: $(window).width() - 655,
        height: 476
    });

    // 图表初始化
    (function () {
        // 车速
        $('#speed-chart').highcharts(getGaugeOptions({
            min: 0,
            max: 180,
            center: ['50%', '65%'],
            size: '125%',
            startAngle: -120,
            endAngle: 120,
            stops: [[0.2, '#2dbbef'],
                [0.4, '#34E259'],
                [0.6, '#FCD821'],
                [0.9, '#FB3D3D']
            ]
        }));
        // 转速
        $('#rpm-chart').highcharts(getGaugeOptions({
            min: 0,
            max: 40,
            center: ['50%', '65%'],
            size: '125%',
            startAngle: -120,
            endAngle: 120,
            stops: [[0.2, '#2dbbef'],
                [0.4, '#34E259'],
                [0.6, '#FCD821'],
                [0.9, '#FB3D3D']
            ]
        }));
        // 电机电压
        $('#djdy-chart').highcharts(getGaugeOptions({
            min: 0,
            max: 400,
            center: ['50%', '50%'],
            size: '100%',
            startAngle: -50,
            endAngle: 50,
            stops: [[400, '#55BF3B']]
        }));
        // 电机电流
        $('#djdl-chart').highcharts(getGaugeOptions({
            min: -600,
            max: 600,
            center: ['50%', '50%'],
            size: '100%',
            startAngle: -50,
            endAngle: 50,
            stops: [[400, '#55BF3B']]
        }));
    })();

    function loadData(data) {
        data = data || {};
        //经纬度
        if (data.longitude && data.latitude) {
            if (bus) {
                bus.clear();
            }
            map.setMapLevel(14);
            bus = new Bus(map, '', data.longitude, data.latitude);
            bus.setDirection(data.direction);
            bus.show();
            map.mapMoveTo(data.longitude, data.latitude);
        }
        // 车速
        var chart = $('#speed-chart').highcharts(), point = chart.series[0].points[0];
        var speedVal = data.speedOfAMotorVehicle || 0;
        point.update(speedVal);
        $('#speed-text').text(speedVal);

        // 转速
        var rpmChart = $('#rpm-chart').highcharts(), rmpPoint = rpmChart.series[0].points[0];
        var rpmVal = (data.electricMachineryRotateSpeed || 0);
        rmpPoint.update(rpmVal / 100);
        $('#rpm-text').text(rpmVal / 100);
        // 电机电压
        var bvcChart = $('#djdy-chart').highcharts(), bvcPoint = bvcChart.series[0].points[0];
        var bvcVal = Number((data.electricMachineryControllerBusBarVoltage || 0).toFixed(1));
        if (bvcVal < 0) {
            bvcVal = 0;
        }
        bvcPoint.update(bvcVal);
        $('#djdy-text').text(bvcVal);

        // 电机电流
        var bccChart = $('#djdl-chart').highcharts(), bccPoint = bccChart.series[0].points[0];
        var bccVal = Number((data.electricMachineryControllerBusBarElectricCurrent || 0).toFixed(1));
        bccPoint.update(bccVal);
        $('#djdl-text').text(bccVal);

        var $state = $('#state');
        var electricMachineryControllerStatus = data.electricMachineryControllerStatus || '111';
        if (electricMachineryControllerStatus == "000") {
            $state.text('READY');
        } else if (electricMachineryControllerStatus == "001") {
            $state.text('RUNNING');
        }
        // 档位gearsStatus
        var gear = data.gears || 0;
        if (gear == 0) {
            $('#gear').text('N档');
        } else if (gear == -1) {
            $('#gear').text('R档');
        } else if (gear == 1) {
            $('#gear').text('D档');
        }

        $('#djzj').text('电机转矩：' + (data.electricMachineryTorque || 0) + 'nm');
        $('#djkzwd').text('电机控制器温度：' + (data.electricMachineryControllerTemperature || 0) + '℃');
        $('#smxh').text('生命(心跳)信号：' + (data.heartBeatSignal || 0));
        $('#dzwd').text('电机温度：' + (data.electricMachineryTemperature || 0) + '℃');
        $('#gzdm').text(data.electricMachineryControllerErrorCode || 0);//故障代码
        if ((electricMachineryControllerStatus).substring(0, 3) == "011") {
            var gzjb = electricMachineryControllerStatus.substring(3, electricMachineryControllerStatus.length);
            if (gzjb == 0) {
                $('#gzjb').text('一级故障');//故障级别
            } else if (gzjb == 1) {
                $('#gzjb').text('二级故障');//故障级别
            } else if (gzjb == 2) {
                $('#gzjb').text('三级故障');//故障级别
            } else if (gzjb == 3) {
                $('#gzjb').text('四级故障');//故障级别
            }
        } else {
            $('#gzjb').text('无');
        }
        $('#dmc-version').text('DMC版本号：' + ((data.dMCVersionNO / 100) || 0));
        $('#eltMhyOprtTotalTime').text("电机累计工作时间：" + data.electricMachineryRunTime);
    }
    
});